package com.zx.article.editor.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.article.editor.dao.IEditorDao;
import com.zx.article.editor.model.EditorModel;
import com.zx.article.util.DBUtils;

public class EditorDaoImpl implements IEditorDao {

	private DBUtils dbUtils = new DBUtils();

	@Override
	public List<EditorModel> queryList(EditorModel editorModel) {
		StringBuffer sqlBuffer = new StringBuffer(
				"SELECT editor.*,(SELECT COUNT(1) FROM review_records WHERE review_records.r_editor_id = editor.e_account) AS count FROM editor ");
		List<Object> appendWhere = appendWhere(sqlBuffer, editorModel);
		Map<String, String> map = getMap();
		map.put("count", "count");
		return dbUtils.queryList(sqlBuffer.toString(), appendWhere.toArray(), EditorModel.class, map);
	}

	private List<Object> appendWhere(StringBuffer sqlBuffer, EditorModel editorModel) {
		List<Object> list = new ArrayList<>();
		sqlBuffer.append("where 1=1 ");
		String account = editorModel.getAccount();
		if (account != null && !account.isEmpty()) {
			sqlBuffer.append("and e_account like ? ");
			list.add(account);
		}
		String realName = editorModel.getRealName();
		if (realName != null && !realName.isEmpty()) {
			sqlBuffer.append("and e_real_name like ? ");
			list.add(realName);
		}
		String penName = editorModel.getNickName();
		if (penName != null && !penName.isEmpty()) {
			sqlBuffer.append("and e_nick_name like ? ");
			list.add(penName);
		}
		if (editorModel.isPageOn()) {
			sqlBuffer.append("limit ?,? ");
			list.add(editorModel.getRowStart());
			list.add(editorModel.getRowCount());
		}
		return list;
	}

	@Override
	public EditorModel querModel(String account) {
		String sqlString = "SELECT * FROM editor WHERE e_account = ? ";
		Object[] valObjects = { account };
		return dbUtils.queryOne(sqlString, valObjects, EditorModel.class, getMap());
	}

	private Map<String, String> getMap() {
		Map<String, String> map = new HashMap<>();
		map.put("account", "e_account");
		map.put("password", "e_password");
		map.put("realName", "e_real_name");
		map.put("nickName", "e_nick_name");
		map.put("image", "e_image");
		map.put("phone", "e_phone");
		map.put("address", "e_address");
		map.put("email", "e_email");
		map.put("message", "e_message");
		return map;
	}

	@Override
	public String deleteModel(String account) {
		String sqlString = "DELETE FROM editor WHERE e_account = ? ";
		Object[] valObjects = { account };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public String insertModel(EditorModel editorModel) {
		String sqlString = "INSERT INTO editor(e_account,e_password,e_real_name,e_nick_name,e_image,e_phone,e_address,e_email,e_message) VALUES(?,?,?,?,?,?,?,?,?)";
		Object[] valObjects = { editorModel.getAccount(), editorModel.getPassword(), editorModel.getRealName(),
				editorModel.getNickName(), editorModel.getImage(), editorModel.getPhone(), editorModel.getAddress(),
				editorModel.getEmail(), editorModel.getMessage() };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public String updataModel(EditorModel editorModel) {
		StringBuffer sqlBuffer = new StringBuffer("UPDATE editor ");
		List<Object> appendSet = appendSet(sqlBuffer, editorModel);
		return dbUtils.update(sqlBuffer.toString(), appendSet.toArray());
	}

	private List<Object> appendSet(StringBuffer sqlBuffer, EditorModel editorModel) {
		List<Object> list = new ArrayList<>();
		String account = editorModel.getAccount();
		list.add(account);
		sqlBuffer.append("set e_account = ?");
		String realName = editorModel.getRealName();
		if (realName != null && !realName.isEmpty()) {
			sqlBuffer.append(",e_real_name = ?");
			list.add(realName);
		}
		String password = editorModel.getPassword();
		if (password != null && !password.isEmpty()) {
			sqlBuffer.append(",e_password = ?");
			list.add(password);
		}
		String nickName = editorModel.getNickName();
		if (nickName != null && !nickName.isEmpty()) {
			sqlBuffer.append(",e_nick_name = ?");
			list.add(nickName);
		}
		String phone = editorModel.getPhone();
		if (phone != null && !phone.isEmpty()) {
			sqlBuffer.append(",e_phone = ?");
			list.add(phone);
		}
		String email = editorModel.getEmail();
		if (email != null && !email.isEmpty()) {
			sqlBuffer.append(",e_email = ?");
			list.add(email);
		}
		String message = editorModel.getMessage();
		if (message != null && !message.isEmpty()) {
			sqlBuffer.append(",e_message = ?");
			list.add(message);
		}
		String image = editorModel.getImage();
		if (image != null && !image.isEmpty()) {
			sqlBuffer.append(",e_image = ?");
			list.add(image);
		}
		sqlBuffer.append(" where e_account = ?");
		list.add(account);
		return list;
	}

	@Override
	public int queryCount(EditorModel editorModel) {
		StringBuffer sqlString = new StringBuffer("SELECT COUNT(1) FROM editor ");
		List<Object> appendWhere = appendWhere(sqlString, editorModel);
		return dbUtils.queryCount(sqlString.toString(), appendWhere.toArray());
	}

}
